/*
 *  GuiTextBox.cpp
 *  SpaceTrader
 *
 *  Created by Jake on 10/7/12.
 *  Copyright 2012 __MyCompanyName__. All rights reserved.
 *
 */

#include "GuiTextBox.h"

GuiTextBox::GuiTextBox(int xVal, int yVal) : x(xVal), y(yVal), ticks(0), inputText(""), text(NULL), isFocused(false)
{
	SDL_EnableUNICODE(SDL_ENABLE);
}

GuiTextBox::~GuiTextBox()
{
	SDL_FreeSurface(text);
	SDL_EnableUNICODE(SDL_DISABLE);
}

GuiTextBox::GuiTextBox(const GuiTextBox &G) : x(G.x), y(G.y), ticks(G.ticks),
                                                inputText(G.inputText), text(G.text), isFocused(false) { }

GuiTextBox& GuiTextBox::operator=(const GuiTextBox &G){
    // check for "self assignment" and do nothing in that case
    if (this == &G) return *this;
    else {
        delete text;
        text = G.text;
        return *this;
    }
}

std::string GuiTextBox::getText()
{
	return inputText;
}

void GuiTextBox::focus(){
    isFocused = true;
}

void GuiTextBox::unFocus(){
    isFocused = false;
}

void GuiTextBox::toggleFocus(){
    isFocused = !isFocused;
}

bool GuiTextBox::hasFocus(){
    return isFocused;
}

textbox_state_t GuiTextBox::handle_input(SDL_Event event, TTF_Font *font, SDL_Color textColor)
{
	if (event.type == SDL_KEYDOWN && isFocused) {
		std::string temp = inputText;
		
		if (inputText.length() <= 16) {
			//if the key is a space
			if (event.key.keysym.unicode == (Uint16)' ') {
				inputText += (char)event.key.keysym.unicode;
			} //If the key is a number
            else if( ( event.key.keysym.unicode >= (Uint16)'0' ) && ( event.key.keysym.unicode <= (Uint16)'9' ) )
            {
                //Append the character
                inputText += (char)event.key.keysym.unicode;
            }
            //If the key is a uppercase letter
            else if( ( event.key.keysym.unicode >= (Uint16)'A' ) && ( event.key.keysym.unicode <= (Uint16)'Z' ) )
            {
                //Append the character
                inputText += (char)event.key.keysym.unicode;
            }
            //If the key is a lowercase letter
            else if( ( event.key.keysym.unicode >= (Uint16)'a' ) && ( event.key.keysym.unicode <= (Uint16)'z' ) )
            {
                //Append the character
                inputText += (char)event.key.keysym.unicode;
            }
        }
		//If backspace was pressed and the inputTexting isn't blank
        if( ( event.key.keysym.sym == SDLK_BACKSPACE ) && ( inputText.length() != 0 ) )
        {
            //Remove a character from the end
            inputText.erase( inputText.length() - 1 );
        }
		//If the inputTexting was changed
        if( inputText != temp )
        {
            //Free the old surface
            SDL_FreeSurface( text );
			
            //Render a new text surface
            text = TTF_RenderText_Blended( font, inputText.c_str(), textColor );
        }
        return TYPE;
    } else if (event.type == SDL_MOUSEBUTTONUP){
        if (event.button.x > this->x && event.button.x < this->x + 200 && event.button.y > this->y && event.button.y < this->y + 25){
            return CLICK;
        }
    }
    return OTHER;
}
			

void GuiTextBox::show(SDL_Surface *screen)
{
        SDL_Rect *rect = new SDL_Rect();
        rect->x = x;
        rect->y = y;
        rect->w = 200;
        rect->h = 25;
        SDL_FillRect(screen, rect,SDL_MapRGB(screen->format, 0x77, 0x77, 0x77));
    
    if (isFocused){
        SDL_Rect *cursor = new SDL_Rect();
        if (text != NULL){
            cursor->x = x + text->w;
        } else {
            cursor->x = x;
        }
        cursor->y = y + 4;
        cursor->w = 2;
        cursor->h = 20;
        if (ticks++ % 75 < 38){
            SDL_FillRect(screen, cursor,SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF));
        }
    }
	apply_surface( x, y, text, screen);
}
